package com.example.helloffmpeg

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.PermissionUtils
import com.example.helloffmpeg.system.FfmpegHelper.decode
import com.example.helloffmpeg.system.FfmpegHelper.decodeAudio
import com.example.helloffmpeg.system.FfmpegHelper.decodeVideo
import com.example.helloffmpeg.system.FfmpegHelper.encode
import com.example.helloffmpeg.system.FfmpegHelper.getVersion
import com.example.helloffmpeg.system.FfmpegHelper.printFileInfo
import com.example.helloffmpeg.system.FfmpegHelper.remux
import com.example.helloffmpeg.util.AssertUtil.copyFile
import com.example.helloffmpeg.util.LogU
import java.io.File

class MainActivity : AppCompatActivity() {
    private var ll_content: LinearLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        requestRead()
    }

    fun initView() {
        ll_content = findViewById(R.id.ll_content)
        addButton("show FFmpeg version") { v: View? ->
            run {
                LogU.d("print file info:${getVersion()}")
            }
        }
        addButton("print file info") { v: View? ->
            val srcFile = copyFile(this)
            if (srcFile != null) {
                Thread{
                    val info = printFileInfo("file:" + srcFile.absolutePath)
                    LogU.d("print file info:$info")
                }.start()
            }
        }
        addButton("file decode to video and audio") { v: View? ->
            val srcFile = copyFile(this)
            if (srcFile != null) {
                val videoFile = File(cacheDir.absolutePath, "sintel.h264")
                val audioFile = File(cacheDir.absolutePath, "sintel.aac")
                if (audioFile.exists()) {
                    audioFile.delete()
                }
                if(videoFile.exists()){
                    videoFile.delete()
                }
                Thread{
                    decode("file:" + srcFile.absolutePath, "file:" +videoFile.absolutePath,"file:" +audioFile.absolutePath)
                }.start()
            }
        }
        addButton("video and audio encode to mp4") { v: View? ->
            val videoFile = File(cacheDir.absolutePath, "sintel.h264")
            val audioFile = File(cacheDir.absolutePath, "sintel.aac")
            val dstFile = File(cacheDir.absolutePath, "sintel1.mp4")
            if(dstFile.exists()){
                dstFile.delete()
            }
            Thread{
//                encode("file:"+videoFile.absolutePath, "file:"+audioFile.absolutePath,dstFile.absolutePath)
                encode(videoFile.absolutePath, audioFile.absolutePath,dstFile.absolutePath)
            }.start()
        }
        addButton("mp4 to flv") { v: View? ->
            val srcFile = copyFile(this)
            if (srcFile != null) {
                val dstFile = File(cacheDir.absolutePath, "sintel.flv")
                if (dstFile.exists()) {
                    dstFile.delete()
                }
                remux("file:" + srcFile.absolutePath, "file:" + dstFile.absolutePath)
            }
        }
        addButton("decode h264 video to yuv") { v: View? ->
            val src = File(cacheDir.absolutePath, "sintel.h264")
            val dst = File(cacheDir.absolutePath, "sintel_frame")
            decodeVideo(src.absolutePath, dst.absolutePath)
        }
        addButton("decode aac audio to pcm") { v: View? ->
            val src = File(cacheDir.absolutePath, "sintel.aac")
            val dst = File(cacheDir.absolutePath, "sintel.pcm")
            decodeAudio(src.absolutePath, dst.absolutePath)
        }
        addButton("list all encoders") { v: View? ->
        }
        addButton("open rtsp player") { v: View? ->
            startActivity(Intent(this,RtspActivity::class.java))
        }
    }

    /**
     * 将asset目录中的媒体文件拷贝到sd卡中
     */
    private fun addButton(name: String, lisener: View.OnClickListener) {
        val lp = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        lp.setMargins(20, 20, 20, 20)
        val button = Button(this)
        button.setPadding(0, 20, 0, 20)
        button.setGravity(Gravity.CENTER)
        button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16f)
        button.text = name
        button.isAllCaps = false
        button.setOnClickListener(lisener)
        ll_content!!.addView(button, lp)
    }

    companion object {
        private const val TAG = "MainActivity"
    }

    private fun requestRead() {
        PermissionUtils.permission(Manifest.permission.READ_EXTERNAL_STORAGE
            ,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET)
            .rationale { activity, shouldRequest ->  }
            .callback(object : PermissionUtils.FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {
                    LogU.d(permissionsGranted)
                }

                override fun onDenied(permissionsDeniedForever: List<String>,
                                      permissionsDenied: List<String>) {
                }
            })
            .theme { activity -> }
            .request()
    }
}
